package com.twang.stock.task.old;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.Entity.ShiShiData;
import com.twang.stock.common.CommonUtils;
import com.twang.stock.common.http.HtmlTools;
import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.entity.StockInfo;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.mapper.StockInfoMapper;
import com.twang.stock.email.SendmailService;
import com.twang.stock.service.jinyan.UpdateStockCoreService;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UpdateToDayVol extends BaseTask{

    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    @Resource
    private StockInfoMapper stockInfoMapper;
    @Autowired
    private SendmailService sendmailService;

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    @Autowired
    private UpdateStockCoreService updateStockCoreService;

   // @Scheduled(cron = "0 57 14 * * ?")
    public void excute() {
        try {
            if (!isWork()) {
                return;
            }
            //更新价格
            updateToDayData();
            //更新市值
            updateStockCoreService.updateCore(1);
            sendmailService.insertEvent(CommonUtils.TASK_NAME_RULE_1, CommonUtils.SUCCESS_MESSAGE, "SUCCESS", CommonUtils.RULE_SUCCESS);
        } catch (Exception e) {
            sendmailService.insertEvent(CommonUtils.TASK_NAME_RULE_1, CommonUtils.FAILE_MESSAGE + JSONObject.toJSONString(e), "ERROR", CommonUtils.RULE_EXCEPTION);
        }
    }


    public void updateToDayData() {
        List<CompanyCodeInfo> listCode = companyCodeInfoMapper.selectAll();
        listCode.forEach(companyCodeInfo -> {
            asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {
                try {
                    querySina(companyCodeInfo);
                } catch (Exception e) {
                    log.info("error = {}", e);
                }
            });
        });
    }

    public StockInfo querySina(CompanyCodeInfo companyCodeInfo) {
        List<String> finalIds = Lists.newArrayList();
        finalIds.add(companyCodeInfo.getCompanyCode());
        List<ShiShiData> listTemp = null;
        try {
            listTemp = HtmlTools.getGpdatas(finalIds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!CollectionUtils.isEmpty(listTemp)) {
            //生成数据
            StockInfo stockInfo = initData(listTemp.get(0));
            return stockInfo;
        } else {
            log.info("sina 未查询到数据  code={},name={}", companyCodeInfo.getCompanyCode(), companyCodeInfo.getShortName());
        }
        return null;
    }


    public StockInfo initData(ShiShiData item) {
        StockInfo info = new StockInfo();
        info.setCode(item.getCode());
        info.setDate(item.getGetdate() + "");
        info.setPrice(new BigDecimal(item.getPrice()).setScale(2,BigDecimal.ROUND_HALF_UP));
        info.setPriceMax(new BigDecimal(item.getMaxprice()).setScale(2,BigDecimal.ROUND_HALF_UP));
        info.setPriceMin(new BigDecimal(item.getMinprice()).setScale(2,BigDecimal.ROUND_HALF_UP));
        info.setVolMay1(item.getSumvol());
        info.setName(item.getGpname());
        info.setCreateTime(new Date());
        info.setZhangdie(item.getZhangdie().setScale(4,BigDecimal.ROUND_HALF_UP));
        StockInfo param = new StockInfo();
        param.setCode(info.getCode());
        param.setDate(info.getDate());
        StockInfo recode = stockInfoMapper.selectOne(param);
        if (null != recode) {
            info.setId(recode.getId());
            info.setVolMay1Max(recode.getVolMay1Max());
            stockInfoMapper.updateByPrimaryKey(info);
        } else {
            stockInfoMapper.insert(info);
        }
        return info;
    }
}
